package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Employee;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.EmployeeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/addEmployee",method = RequestMethod.POST)
        public Result addEmployee(Employee employee){
        try {
            String salt = System.currentTimeMillis()+"";
            Md5Hash md5Hash = new Md5Hash(employee.getEmpPassword(), salt, 1024);
            String newPassword = md5Hash.toString();//加密后密码
            employee.setEmpPassword(newPassword);
            employee.setSalt(salt);
            employeeService.addEmployee(employee);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除员工
     * @param empId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/deleteEmployee")
    public Result deleteEmployee(Integer empId){
        try {
            employeeService.deleteEmployee(empId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新员工
     * @param employee
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/updateEmployee",method = RequestMethod.POST)
    public Result updateEmployee(Employee employee){
        try {
            if (employee.getEmpPassword()!=null){
                String salt = System.currentTimeMillis()+"";
                Md5Hash md5Hash = new Md5Hash(employee.getEmpPassword(), salt, 1024);
                String newPassword = md5Hash.toString();//加密后密码
                employee.setEmpPassword(newPassword);
                employee.setSalt(salt);
            }
            employeeService.updateEmployee(employee);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 根据ID获取员工信息
     * @param empId
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/getEmployeeByEmpId")
    public Result getEmployeeByEmpId(Integer empId){
        try {
            Employee employeeByEmployeeId = employeeService.getEmployeeByEmployeeId(empId);
            System.out.println(employeeByEmployeeId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(employeeByEmployeeId);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = {"manager"})
    @RequestMapping(value = "/getEmployeePage")
    public Result getEmployeePage(Integer pageNo,Integer pageSize){
        try {
            Page employeePage = employeeService.getEmployeePage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(employeePage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }



}
